package com.sgb.kjwl.view.ui.adapter.transport;


import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.MyCarInfoEntity;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.UserOperating;

import java.util.List;

/**
 * @author: 杨畔
 * @date: 2021/10/9 11:02
 * @Description: 自营车辆管理适配器
 */
public class SelfCarManagerAdapter extends BaseQuickAdapter<MyCarInfoEntity, BaseViewHolder> {
    public SelfCarManagerAdapter(@Nullable List<MyCarInfoEntity> data) {
        super(R.layout.app_item_self_car_manager, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, MyCarInfoEntity item) {
        //团队隐藏车队信息
        if (UserOperating.getInstance().isTeam()) {
            helper.getView(R.id.group_fleet).setVisibility(View.INVISIBLE);
        } else {
            helper.getView(R.id.group_fleet).setVisibility(View.VISIBLE);
        }
        //车辆图片
        Glide.with(mContext).load(DefaultUtil.getString(item.getCarImage()))
                .placeholder(R.mipmap.ic_default_car)
                .error(R.mipmap.ic_default_car)
                .into((ImageView) helper.getView(R.id.iv_car_image));
        //团队车辆无车队信息
        helper.setText(R.id.tv_car_number, DefaultUtil.getString(item.getCarNumber()))
                .setText(R.id.tv_driver, DefaultUtil.getString(item.getDriverName(),"/"))
                .setText(R.id.tv_fleet, DefaultUtil.getString(item.getMotorcadeName(),"/"))
                .setVisible(R.id.tv_bind, TextUtils.isEmpty(item.getDriverNo()))
                .setVisible(R.id.tv_unbind, !TextUtils.isEmpty(item.getDriverNo()))
                .addOnClickListener(R.id.cl_item)
                .addOnClickListener(R.id.tv_bind)
                .addOnClickListener(R.id.tv_unbind);
        //车辆信息
        if (item.getIsComplete() == 1) {
            StringBuilder carBuilder = new StringBuilder();
            if (!TextUtils.isEmpty(item.getCarTypeName())) {
                carBuilder.append("/").append(item.getCarTypeName());
            }
            if (!TextUtils.isEmpty(item.getCarLenName())) {
                carBuilder.append("/长").append(item.getCarLenName());
            }
            if (item.getWeight() != null) {
                carBuilder.append("/");
                carBuilder.append(NumberUtil.trim0(item.getWeight()));
                carBuilder.append("吨");
            }
            helper.setTextColor(R.id.tv_car_info, ContextCompat.getColor(mContext, R.color.color_tab_one))
                    .setText(R.id.tv_car_info, carBuilder.length() > 0
                            ? carBuilder.substring(1)
                            : "");
        } else {
            helper.setTextColor(R.id.tv_car_info, ContextCompat.getColor(mContext, R.color.color_FF4B10))
                    .setText(R.id.tv_car_info, "车辆信息待完善");
        }
        //没有位置信息时隐藏
        if (TextUtils.isEmpty(item.getProvinceName())
                && TextUtils.isEmpty(item.getCityName())
                && TextUtils.isEmpty(item.getDistrictName())) {
            helper.setVisible(R.id.tv_address, false);
        } else {
            helper.setVisible(R.id.tv_address, true)
                    .setText(R.id.tv_address, String.format("%s%s%s",
                            item.getProvinceName(),
                            item.getCityName(),
                            item.getDistrictName()));
        }
        //上下架 1 上架 0 下架
        if (item.getState() == 0) {
            helper.setText(R.id.tv_car_certification, "已下架")
                    .setTextColor(R.id.tv_car_certification, ContextCompat.getColor(mContext, R.color.color_969696));
            return;
        }
        //审核状态（0 待审核 1 审核通过 2 审核不通过）
        switch (item.getStatus()) {
            case 0:
                helper.setText(R.id.tv_car_certification, "审核中")
                        .setTextColor(R.id.tv_car_certification, ContextCompat.getColor(mContext, R.color.color_5792FD));
                break;
            case 1:
                helper.setText(R.id.tv_car_certification, "审核通过")
                        .setTextColor(R.id.tv_car_certification, ContextCompat.getColor(mContext, R.color.color_06C764));
                break;
            case 2:
                helper.setText(R.id.tv_car_certification, "审核不通过")
                        .setTextColor(R.id.tv_car_certification, ContextCompat.getColor(mContext, R.color.color_FF4B10));
                break;
            default:
                helper.setText(R.id.tv_car_certification, "");
                break;
        }
    }
}
